<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name" >
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="帧类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择帧类型" clearable>
          <el-option
            v-for="dict in dict.type.dict_frame"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预读长度" prop="preReadLen" v-show="false">
        <el-input
          v-model="queryParams.preReadLen"
          placeholder="请输入预读长度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="协议标志" prop="markFlag" >
        <el-input
          v-model="queryParams.markFlag"
          placeholder="请输入协议标志"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="协议标志下标" prop="markIndexList" v-show="false">
        <el-input
          v-model="queryParams.markIndexList"
          placeholder="请输入协议标志下标"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="帧长度" prop="len" v-show="false">
        <el-input
          v-model="queryParams.len"
          placeholder="请输入帧长度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="帧长度下标" prop="lenIndexList" v-show="false">
        <el-input
          v-model="queryParams.lenIndexList"
          placeholder="请输入帧长度下标"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="其他长度" prop="otherLen" v-show="false">
        <el-input
          v-model="queryParams.otherLen"
          placeholder="请输入其他长度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="左向量索引" prop="dataLeftVector" v-show="false">
        <el-input
          v-model="queryParams.dataLeftVector"
          placeholder="请输入数据左向量索引"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="右向量索引" prop="dataRightVector" v-show="false">
        <el-input
          v-model="queryParams.dataRightVector"
          placeholder="请输入数据左向量索引"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据类型" prop="fieldType">
        <el-select v-model="queryParams.fieldType" placeholder="请选择数据类型" clearable>
          <el-option
            v-for="dict in dict.type.iot_field_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['iot:frame:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['iot:frame:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['iot:frame:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['iot:frame:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="frameList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="协议帧id" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="帧类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dict_frame" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="预读长度" align="center" prop="preReadLen" />
      <el-table-column label="协议标志" align="center" prop="markFlag" />
      <el-table-column label="协议标志下标" align="center" prop="markIndexList" />
      <el-table-column label="帧长度" align="center" prop="len" />
      <el-table-column label="帧长度下标" align="center" prop="lenIndexList" />
      <el-table-column label="其他长度" align="center" prop="otherLen" />
      <el-table-column label="左向量索引" align="center" prop="dataLeftVector" />
      <el-table-column label="右向量索引" align="center" prop="dataRightVector" />
      <el-table-column label="数据类型" align="center" prop="fieldType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.iot_field_type" :value="scope.row.fieldType"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['iot:frame:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['iot:frame:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改厂家协议对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="帧类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择帧类型">
            <el-option
              v-for="dict in dict.type.dict_frame"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预读长度" prop="preReadLen">
          <el-input v-model="form.preReadLen" placeholder="请输入预读长度" />
        </el-form-item>
        <el-form-item label="协议标志" prop="markFlag">
          <el-input v-model="form.markFlag" placeholder="请输入协议标志" />
        </el-form-item>
        <el-form-item label="协议下标" prop="markIndexList">
          <el-input v-model="form.markIndexList" placeholder="请输入协议标志下标" />
        </el-form-item>
        <el-form-item label="帧长度" prop="len">
          <el-input v-model="form.len" placeholder="请输入帧长度" />
        </el-form-item>
        <el-form-item label="帧下标" prop="lenIndexList">
          <el-input v-model="form.lenIndexList" placeholder="请输入帧长度下标" />
        </el-form-item>
        <el-form-item label="其他长度" prop="otherLen">
          <el-input v-model="form.otherLen" placeholder="请输入其他长度" />
        </el-form-item>
        <el-form-item label="左向量索引" prop="dataLeftVector">
          <el-input v-model="form.dataLeftVector" placeholder="请输入数据左边的索引" />
        </el-form-item>
        <el-form-item label="右向量索引" prop="dataRightVector">
          <el-input v-model="form.dataRightVector" placeholder="请输入数据右边的索引" />
        </el-form-item>
        <el-form-item label="数据类型" prop="fieldType">
          <el-select v-model="form.fieldType" placeholder="请选择数据类型">
            <el-option
              v-for="dict in dict.type.iot_field_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFrame, getFrame, delFrame, addFrame, updateFrame } from "@/api/iot/frame";

export default {
  name: "Frame",
  dicts: ['iot_field_type', 'dict_frame'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 厂家协议表格数据
      frameList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        preReadLen: null,
        markFlag: null,
        markIndexList: null,
        len: null,
        lenIndexList: null,
        otherLen: null,
        dataLeftVector: null,
        dataRightVector: null,
        fieldType: null,
        creator: null,
        createDate: null,
        updater: null,
        updateDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询厂家协议列表 */
    getList() {
      this.loading = true;
      listFrame(this.queryParams).then(response => {
        this.frameList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        preReadLen: null,
        markFlag: null,
        markIndexList: null,
        len: null,
        lenIndexList: null,
        otherLen: null,
        dataLeftVector: null,
        dataRightVector: null,
        fieldType: null,
        remark: null,
        creator: null,
        createDate: null,
        updater: null,
        updateDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加厂家协议";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFrame(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改厂家协议";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFrame(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFrame(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除厂家协议编号为"' + ids + '"的数据项？').then(function() {
        return delFrame(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('iot/frame/export', {
        ...this.queryParams
      }, `frame_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
